package com.ax.base.common.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ax.base.common.dao.BaseOrgMapper;
import com.ax.base.common.dao.DoctorPharmacistMapper;
import com.ax.base.common.domain.BaseDoctor;
import com.ax.base.common.domain.BasePharmacist;
import com.ax.base.common.domain.Tree;
import com.ax.base.common.service.DoctorPharmacistService;
import com.ax.base.common.utils.BuildTree;
import com.ax.base.common.utils.MD5Utils;
import com.ax.base.common.utils.PageUtils;
import com.ax.base.common.utils.Query;
import com.ax.base.system.domain.MenuDO;
import com.ax.base.system.domain.SysUserDO;

@Service
public class DoctorPharmacistServiceImpl implements DoctorPharmacistService {
	@Autowired
	BaseOrgMapper baseOrgMapper;
	@Autowired
	DoctorPharmacistMapper doctorPharmacistMapper;
	
	/**
	 * 查看医生详情(模块:医生呈现，医生详情*查询某科室下面的医生)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@Override
	public List<BaseDoctor> queryBaseDoctorSer(Map<String, Object> params) {
	//	Query query = new Query(params);
		List<BaseDoctor> BaseDoctors = doctorPharmacistMapper.queryBaseDoctorMapper(params);
	//	Integer count = doctorPharmacistMapper.baseDoctorCount(query);
	//	PageUtils pageUtil = new PageUtils(BaseDoctors,count);
		return BaseDoctors;
	}

	/**
	 * 组织机构树
	 */
	@Override
	public Tree<MenuDO> queryorgtreeSer() {
		List<Tree<MenuDO>> deps = baseOrgMapper.listOrg();
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * 新增药师详情(模块:药师)
	 * 
	 * @param BasePharmacist basePharmacist
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertBasePharmacistSer(Map<String, Object> params,BasePharmacist basePharmacist) {
		doctorPharmacistMapper.insertBasePharmacistMapper(basePharmacist);
		SysUserDO sysUser = new SysUserDO();
		sysUser.setUsername(basePharmacist.getPhone());
		sysUser.setPassword(MD5Utils.encrypt(basePharmacist.getPhone(),"123456"));
		sysUser.setMobile(basePharmacist.getPhone());
		sysUser.setStatus(1);
		sysUser.setName(basePharmacist.getPharmacistName());
		sysUser.setUserTypeCode("02");
		sysUser.setUserTypeName("药师");
		sysUser.setUserCode(basePharmacist.getPharmacistId());
		sysUser.setFlag(1);
		doctorPharmacistMapper.insertUser(sysUser);
	}

	/**
	 * 查看药师详情(模块:药师，药师详情)
	 * 
	 * @param BasePharmacist basePharmacist
	 *           
	 * @return PageUtils
	 */
	@Override
	public List<BasePharmacist> queryBasePharmacistSer(Map<String, Object> params) {
	//	Query query = new Query(params);
		List<BasePharmacist> BasePharmacists = doctorPharmacistMapper.selectByPrimaryKey(params);
	//	Integer count = doctorPharmacistMapper.count(query);
	//	PageUtils pageUtil = new PageUtils(BasePharmacists,count);
		return BasePharmacists;
	}

	/**
	 * 查看药师详情(修改)
	 * 
	 * @param Long id
	 *           
	 * @return SUCC
	 */
	@Override
	public BasePharmacist getPharmacistSer(Long id) {
		return doctorPharmacistMapper.getPharmacistMapper(id);
	}

	/**
	 * 修改药师详情(模块:药师)
	 * 
	 * @param BasePharmacist basePharmacist
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateBasePharmacistSer(Map<String,Object> params) {
		doctorPharmacistMapper.updateBasePharmacistMapper(params);
	}

	/**
	 * 组织机构树(编辑)
	 */
	@Override
	public Tree<MenuDO> getTree(Long orgId){
		String pOrgId = orgId.toString();
		List<Tree<MenuDO>> deps = baseOrgMapper.listOrg();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (pOrgId.contains(id)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * 新增医生详情(模块:医生)
	 * 
	 * @param BaseDoctor baseDoctor
	 *           
	 * @return SUCC
	 */
	@Override
	@Transactional
	public void insertBaseDoctorSer(BaseDoctor baseDoctor) {
		String subjectName = baseDoctor.getSubjectName().substring(0,baseDoctor.getSubjectName().indexOf("-"));
		String subjectCode = baseDoctor.getSubjectName().substring(baseDoctor.getSubjectName().indexOf("-") + 1);
		baseDoctor.setSubjectName(subjectName);
		baseDoctor.setSubjectCode(subjectCode);
		doctorPharmacistMapper.insertBaseDoctorMapper(baseDoctor);
		SysUserDO sysUser = new SysUserDO();
		sysUser.setUsername(baseDoctor.getPhone());
		sysUser.setPassword(MD5Utils.encrypt(baseDoctor.getPhone(),"123456"));
		sysUser.setMobile(baseDoctor.getPhone());
		sysUser.setStatus(1);
		sysUser.setName(baseDoctor.getDoctorName());
		sysUser.setUserTypeCode("01");
		sysUser.setUserTypeName("医师");
		sysUser.setUserCode(baseDoctor.getDoctorId());
		sysUser.setFlag(1);
		doctorPharmacistMapper.insertUser(sysUser);
	}

	/**
	 * 修改医生详情(模块:医生)
	 * 
	 * @param BaseDoctor baseDoctor
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateBaseDoctorSer(BaseDoctor baseDoctor) {
		String subjectName = baseDoctor.getSubjectName().substring(0,baseDoctor.getSubjectName().indexOf("-"));
		String subjectCode = baseDoctor.getSubjectName().substring(baseDoctor.getSubjectName().indexOf("-") + 1);
		baseDoctor.setSubjectName(subjectName);
		baseDoctor.setSubjectCode(subjectCode);
		doctorPharmacistMapper.updateBaseDoctorMapper(baseDoctor);
	}
	
	/**
	 * 查看医师详情(修改)
	 * 
	 * @param Long id
	 *           
	 * @return SUCC
	 */
	@Override
	public BaseDoctor getDoctorSer(Long id) {
		return doctorPharmacistMapper.getDoctorMapper(id);
	}

	/**
	 * 医师是否重复(新增)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@Override
	public boolean doctorValidateSer(Map<String, Object> params) {
		boolean validate;
		validate = doctorPharmacistMapper.doctorValidateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 医师是否重复(修改)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@Override
	public boolean updateDoctorValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		String[] pts = pt.split(",");
		params.put("doctorId",pts[1]);
		if(pts[2].equals("personId")){
		params.put("personId",pts[0]);
		params.put("empno",null);
		}else{
		params.put("empno",pts[0]);
		params.put("personId",null);
		}
		boolean validate;
		validate = doctorPharmacistMapper.updateDoctorValidateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 药师是否重复(新增)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@Override
	public boolean pharmacistValidateSer(Map<String, Object> params) {
		boolean validate;
		validate = doctorPharmacistMapper.pharmacistValidateMapper(params).size() > 0;
		return validate;
	}

	@Override
	public boolean updatePharmacistValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		String[] pts = pt.split(",");
		params.put("pharmacistId",pts[1]);
		if(pts[2].equals("personId")){
		params.put("personId",pts[0]);
		params.put("empno",null);
		}else{
		params.put("empno",pts[0]);
		params.put("personId",null);
		}
		boolean validate;
		validate = doctorPharmacistMapper.updatePharmacistValidateMapper(params).size() > 0;
		return validate;
	}
	
}
